package com.example.utils

import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager

import android.provider.MediaStore
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.app.ActivityCompat.requestPermissions

import androidx.core.app.ActivityCompat.startActivityForResult
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.bumptech.glide.Glide


import com.example.demo1.R
import com.example.demo1.fragment.BaseFragment
import com.google.android.material.bottomsheet.BottomSheetDialog

fun loadImage(context: Context, imageView: ImageView, imageUrl: String) {
    Glide.with(context)
        .load(imageUrl)
        .into(imageView)
}

fun checkPermission(context:Context, permissions: Array<String>, code:Int){
    requestPermissions(context as Activity,permissions,code)
}




fun showBottomSheetDialog(context: Context) {
    // 创建 BottomSheetDialog
    val bottomSheetDialog = BottomSheetDialog(context)

    // 加载自定义布局
    val view: View = LayoutInflater.from(context).inflate(R.layout.dialog_button_sheet, null)

    // 设置内容视图
    bottomSheetDialog.setContentView(view)

    // 找到布局中的控件并设置点击事件
    val takePhoto = view.findViewById<TextView>(R.id.takePhoto)
    val chooseFromGallery = view.findViewById<TextView>(R.id.chooseFromGallery)
    val cancel = view.findViewById<TextView>(R.id.cancel)

    takePhoto.setOnClickListener {
        checkPermission(context, arrayOf(Manifest.permission.CAMERA),1)
        Toast.makeText(context, "拍照", Toast.LENGTH_SHORT).show()
//        context.startActivity(Intent(MediaStore.ACTION_IMAGE_CAPTURE))
//        openCamera(context)
        bottomSheetDialog.dismiss()
    }

    chooseFromGallery.setOnClickListener {
        checkPermission(context, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE),2)
        Toast.makeText(context, "从相册选择", Toast.LENGTH_SHORT).show()
//        context.startActivity(
//            Intent(
//                Intent.ACTION_PICK,
//                MediaStore.Images.Media.EXTERNAL_CONTENT_URI
//            )
//        )
        bottomSheetDialog.dismiss()
    }

    cancel.setOnClickListener {
        bottomSheetDialog.dismiss()
    }

    // 显示对话框
    bottomSheetDialog.show()
}

private const val REQUEST_IMAGE_CAPTURE = 1
private const val CAMERA_PERMISSION_REQUEST_CODE = 2

fun openCamera(context: Context) {
    if (ContextCompat.checkSelfPermission(
            context,
            android.Manifest.permission.CAMERA
        ) == PackageManager.PERMISSION_GRANTED
    ) {
        val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        if (takePictureIntent.resolveActivity(context.packageManager) != null) {
            if (context is Activity) {
                context.startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
            } else if (context is Fragment && context.activity != null) {
                context.startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
            }
        }
    } else {
        requestCameraPermission(context)
    }
}

private fun requestCameraPermission(context: Context) {
    if (context is Activity) {
        ActivityCompat.requestPermissions(
            context,
            arrayOf(android.Manifest.permission.CAMERA),
            CAMERA_PERMISSION_REQUEST_CODE
        )
    } else if (context is Fragment && context.activity != null) {
        context.requestPermissions(
            arrayOf(android.Manifest.permission.CAMERA),
            CAMERA_PERMISSION_REQUEST_CODE
        )
    }
}

fun onRequestPermissionsResult(
    context: Context,
    requestCode: Int,
    permissions: Array<String>,
    grantResults: IntArray
) {
    if (requestCode == CAMERA_PERMISSION_REQUEST_CODE) {
        if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            openCamera(context)
        }
    }
}




